package http

import (
	"gitee.com/simple-set/simple.io/src/process/codec"
	"net/http"
)

// Request http请求体
type Request struct {
	http.Request
	Response  *Response
	body      *Body
	BufReadr  *codec.ByteBuf
	BufWriter *codec.ByteBuf
}

func (r *Request) Body() *Body {
	return r.body
}

func (r *Request) SetBody(body *Body) {
	r.body = body
}

func (r *Request) AddHeader(name, value string) {
	if r.Header == nil {
		r.Header = make(http.Header, 4)
	}
	if r.Header.Get(name) != "" {
		r.Header.Add(name, value)
	} else {
		r.Header.Set(name, value)
	}
}

func (r *Request) AddCookie(name, value string) {
	r.AddCookieEntity(&http.Cookie{Name: name, Value: value})
}

func (r *Request) AddCookieEntity(cookie *http.Cookie) {
	if r.Header == nil {
		r.Header = make(http.Header, 4)
	}
	r.Request.AddCookie(cookie)
}

func NewRequest(readBuff *codec.ByteBuf) *Request {
	return &Request{BufReadr: readBuff}
}
